
from django import http

# 定义一个装饰器 完成判断用户是否登录验

def login_required(view):
    def wrapper(request,*args,**kwargs):
        if not request.user.is_authenticated:
            return http.JsonResponse({'code': 400,'errmsg': '未登陆！'})

        return view(request,*args,**kwargs)

    return  wrapper


# 重写 as_view()方法

class LoginRequiredMixin(object):
    @ classmethod
    def as_view(cls,*args,**kwargs):

        view = super().as_view(*args,**kwargs)

        view = login_required(view)

        return view